package com.itheima.controller;


import com.itheima.common.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/getBusinessReportData")
    public Result exportBusinessReport(){
        Report report =reportService.getBusinessReportData();
        return new Result(true,"获取运营统计数据成功",report);
    }
    @GetMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 取出数据
            Report report = reportService.getBusinessReportData();

            // 设置文件路径
            String filePath = "D:\\daima1\\bxg-health-parent\\bxg-health-backend\\src\\main\\resources\\templates\\report_template.xlsx";

            // 读取模板文件
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            XSSFWorkbook excel = new XSSFWorkbook(fis);

            XSSFSheet sheet = excel.getSheetAt(0); // 获取工作表

            // 设置报告日期
            XSSFRow row = sheet.getRow(2);
            XSSFCell cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getReportDate().toString());

            // 设置新增会员数
            row = sheet.getRow(4);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getTodayNewMember());

            // 设置总会员数
            row = sheet.getRow(4);
            cell = row.getCell(7);
            if (cell == null) {
                cell = row.createCell(7);
            }
            cell.setCellValue(report.getTotalMember());

            // 设置本周新增会员数
            row = sheet.getRow(5);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getThisWeekNewMember());

            // 设置今日预约数
            row = sheet.getRow(6);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getTodayVisitsNumber());

            // 设置本周预约数
            row = sheet.getRow(7);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getThisWeekVisitsNumber());

            // 设置本月预约数
            row = sheet.getRow(8);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getThisMonthOrderNumber());

            // 设置本月到诊数
            row = sheet.getRow(9);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getThisMonthVisitsNumber());

            // 设置本月新增会员数
            row = sheet.getRow(10);
            cell = row.getCell(5);
            if (cell == null) {
                cell = row.createCell(5);
            }
            cell.setCellValue(report.getThisMonthNewMember());

            // 设置热门套餐
            List<hotSetmeal> hotSetmealList = report.getHotSetmeal();
            int rowIndex = 13; // 假设热门套餐从第13行开始
            for (hotSetmeal hotSetmeal : hotSetmealList) {
                row = sheet.getRow(rowIndex);
                if (row == null) {
                    row = sheet.createRow(rowIndex);
                }

                cell = row.getCell(0);
                if (cell == null) {
                    cell = row.createCell(0);
                }
                cell.setCellValue(hotSetmeal.getName());

                cell = row.getCell(1);
                if (cell == null) {
                    cell = row.createCell(1);
                }
                cell.setCellValue(hotSetmeal.getSetmeal_count());

                rowIndex++;
            }

            // 输出 Excel 文件
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=report_" + report.getReportDate().toString() + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();

            fis.close();
            excel.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        LocalDate now = LocalDate.from(LocalDateTime.now());
        LocalDate start = LocalDate.from(LocalDateTime.of(now.getYear(), now.getMonth(), 1, 0, 0));
        LocalDate end = LocalDate.from(LocalDateTime.of(now.getYear(), now.getMonth(), now.getMonth().length(now.isLeapYear()), 23, 59));
        List<LocalDate> yue = reportService.getMemberReportMonth(start, end);
        List<Integer> memberCount = reportService.getMemberReport(yue);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
        List<String> months = yue.stream()
                .map(localDate -> localDate.format(formatter))
                .collect(Collectors.toList());


        return new Result(true,"获取会员统计数据成功",new ReportMember(months,memberCount));
    }
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        List<Setmeal> setmealList = reportService.getSetmealReport();
        List<String> setmealNames = setmealList.stream().map(Setmeal::getName).collect(Collectors.toList());
        List<SetmealCount> setmealCount = reportService.getSetmealReportCount();
        return new Result(true,"获取套餐统计数据成功",new SetmealReport(setmealNames,setmealCount));
    }



}
